package org.lgl.search.model;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.lgl.goods.model.Goods;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lgl
 * @date 2022/2/25 11:37 PM
 */
@Data
public class GoodsEsInfo {
    @Id
    private Long id;

    /**
     * @Field 字段的映射 , 注解无效，需要json
     * analyzer 指定索引的是用的分词分词器   searchAnalyzer :搜索的时候使用的分词器
     * type 指定数据类型
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String name;

    /**
     * 为了把类似 华为手机 这种搜索结果排在最前面（既包含华为也包含手机）
     */
    private String fullName;

    private Long price;

    private Long stock;

    /**
     * 不用分词， 且不用建立索引
     */
    private String image;

    private Long categoryId;

    private Long brandId;

    /**
     * FieldType.Keyword 是一个关键字 (keyword) 表示不分词.
     */
    private String brandName;

    /**
     * 用 ik_max_word, 在搜索 电视 时也能匹配到 平板电视， ik_smart 则不行
     */
    private String categoryName;

    private String specsJson;

    private String paramJson;

    private Date createTime;
    private Date updateTime;

    private int status;

    private Map<String, Object> specsMap = new HashMap<>(16);

    public GoodsEsInfo(Goods goods) {
        this.id = goods.getId();
        this.name = goods.getName();
        this.fullName = goods.getName() + " " + goods.getCategoryName() + " " + goods.getBrandName();
        this.price = goods.getPrice();
        this.stock = goods.getStock();
        this.image = goods.getImage();
        this.categoryId = goods.getCategoryId();
        this.brandId = goods.getBrandId();
        this.categoryName = goods.getCategoryName();
        this.brandName = goods.getBrandName();
        this.specsJson = goods.getSpecsJson();
        this.paramJson = goods.getParamJson();
        this.createTime = goods.getCreateTime();
        this.updateTime = goods.getUpdateTime();
        this.status = goods.getStatus();
        this.specsMap = JSON.parseObject(goods.getSpecsJson(), Map.class);
    }
}
